package com.zwz.service.impl;

import com.zwz.dto.AdministrativePenaltyDTO;
import com.zwz.entity.AdministrativePenalty;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.entity.RiskInfoMain;
import com.zwz.mapper.AdministrativePenaltyMapper;
import com.zwz.mapper.RiskInfoMainMapper;
import com.zwz.service.AdministrativePenaltyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AdministrativePenaltyServiceImpl implements AdministrativePenaltyService {

    @Autowired
    private RiskInfoMainMapper mainMapper;

    @Autowired
    private AdministrativePenaltyMapper penaltyMapper;

    @Override
    public AdministrativePenaltyDTO createWithMain( String enterpriseId) {
        // 1. 创建主表记录
//        RiskInfoMain main = new RiskInfoMain();
//        main.setCompanyId(1);
//        mainMapper.insert(main);

        //1.创建主表记录.为了方面暂时使用这个原表
      EnterpriseInfo  enterpriseInfo=new EnterpriseInfo();

      enterpriseInfo.setId(Long.valueOf(enterpriseId));

        // 2. 设置子表关联ID



        AdministrativePenalty penalty = new AdministrativePenalty();

        int k=0;
        penalty.setId(k++);
        penalty.setPenaltyCount(1);
        penalty.setAuthority("你好哦");

        // 3. 校验处罚日期逻辑
        validatePenaltyDates(penalty);
        //5.关联主模型
        penalty.setEnterpriseId(Math.toIntExact(enterpriseInfo.getId()));
        // 4. 插入处罚数据
        penaltyMapper.insert(penalty);


        return new AdministrativePenaltyDTO(enterpriseInfo, penalty);
    }

    @Override
    public void updatePenalty(AdministrativePenalty penalty) {

    }

    @Override
    public List<AdministrativePenalty> getByDateRange(Date start, Date end) {
        return null;
    }

    private void validatePenaltyDates(AdministrativePenalty penalty) {
        if (penalty.getDecisionDate() != null &&
                penalty.getPublishDate() != null &&
                penalty.getPublishDate().before(penalty.getDecisionDate())) {
            throw new IllegalArgumentException("公示日期不能早于决定日期");
        }
    }
}


